/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author computer
 */
public class admindelmsg extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */

    int UID, msgID, ownerID, isAdmin = 0;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException, Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {       
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Threadr || Deleting message...</title>");
            out.println("</head>");
            out.println("<body>");
            
            Connect.init();
            Connection conn = Connect.getConnection();
            HttpSession session = request.getSession(true);
            Statement stat = conn.createStatement();
            Statement stat2 = conn.createStatement();
            Statement stat3 = conn.createStatement();

            msgID = Integer.parseInt(request.getParameter("id"));

            if (session.getAttribute("uID")!=null)
                UID = (Integer)session.getAttribute("uID");
            else {
                out.println("NOT LOGGED IN");
                out.println("</body>");
                out.println("</html>");
            }

            try
            {
                ResultSet user = stat.executeQuery("SELECT * FROM t_Users WHERE UID = "+UID);
                while (user.next())
                    isAdmin = user.getInt("isAdmin");
                ResultSet msg = stat2.executeQuery("SELECT * FROM t_Messages WHERE messageID = "+msgID);
                while (msg.next())
                    ownerID = msg.getInt("ownerUID");
                if (isAdmin == 0 && UID != ownerID){
                    out.println("INVALID ACCESS");
                    out.println("</body>");
                    out.println("</html>");
                }
                stat3.executeUpdate("UPDATE t_Messages SET deleted = 1 WHERE messageID = "+msgID);
            }
            finally
            {
             out.println("MESSAGE DELETED");
             conn.close();
            }           
        } finally {
            out.println("<br/><a href=\"index\">Back to homepage</a>");
            out.println("</body>");
            out.println("</html>");
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    //@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    //@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Login Servlet";
    }// </editor-fold>
}